package com.example.weather_news.fragment;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import com.android.volley.Response;
import com.example.weather_news.R;
import com.example.weather_news.activity.SearchNewsActivity;
import com.example.weather_news.fragment.frag_items.Frag_News;
import com.example.weather_news.http.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Fragment_News extends BaseFragment {


    private RelativeLayout relative_search;
    private ViewPager vp_news;
    private TabLayout tablayout;

    private NewsPagerAdapter adapter;

    private List<String> lists = new ArrayList<String>();

    @Override
    public void initView() {
        relative_search = findView(R.id.relative_search);
        tablayout = findView(R.id.tablayout);
        vp_news = findView(R.id.vp_news);
    }

    @Override
    public void initData() {
        setStatusBarColor();
        adapter = new NewsPagerAdapter(getActivity().getSupportFragmentManager());
        vp_news.setAdapter(adapter);
        tablayout.setupWithViewPager(vp_news);
        getNewsChannel();

        relative_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchNewsActivity.class));
                getActivity().
                        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });

    }

    private void getNewsChannel() {
        HttpRequest.requestNewsChannel(new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.getInt("status") != 0) {
                        showToast(jsonObject.getString("msg"));
                    } else {
                        JSONArray array = new JSONArray(jsonObject.getString("result"));
                        for (int i = 0; i < array.length(); i++) {
                            lists.add((String) array.get(i));
                            if (lists.size() == array.length()) {
                                Log.e("lists", lists.toString());
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, null);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
            setStatusBarColor();
        }
    }
    //设置沉浸栏颜色
    private void setStatusBarColor(){
        this.getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color
                .blue_check));
    }

    class NewsPagerAdapter extends FragmentPagerAdapter {

        public NewsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (lists.get(position) != null) {
                return Frag_News.newInstance(lists.get(position));
            }
            return null;
        }

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return lists.get(position);
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_news;
    }

}
